package com.gztms.job;

import com.gztms.enums.OrderStatus;
import com.gztms.ordermgnt.mapper.OrderMapper;
import com.gztms.ordermgnt.pojo.Order;
import com.gztms.ordermgnt.service.OrderService;
import com.gztms.travelmgnt.pojo.Attractions;
import com.gztms.travelmgnt.service.AttractionsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author Zoey
 * @date 2023/5/2 22:08
 * @Description TODO
 **/
@Component
@Slf4j
public class OrderDlxConsumer {

    @Autowired
    private OrderService orderService;
    @Autowired
    private AttractionsService attractionsService;

    /**
     * 监听我们的死信队列
     */
    @RabbitListener(queues = "mayikt_order_dlx_queue")
    public void orderConsumer(String orderId) {
        System.out.println("死信队列获取消息:" + orderId);
        if (StringUtils.isEmpty(orderId)) {
            return;
        }
        //根据id查询
        Order order = orderService.getById(orderId);
        if (null == order) {
            return;
        }
        //获取状态
        Integer orderStatus=order.getStatus();
        //判断未支付 , 关闭订单
        if(OrderStatus.ORDER_UNPAID == orderStatus){
            order.setStatus(OrderStatus.ORDER_CANCEL);
            orderService.updateById(order);
            // 库存返还
            Attractions attraction = attractionsService.getById(order.getAttrId());
            attraction.setRemainSum(attraction.getRemainSum()+order.getCount());
            log.info("订单超时未支付！返回库存");
        }
    }
}
